#include "Preprocessor.hpp"
#include <iostream>

std::string Preprocessor::Preprocess(std::string Pattern) {
	std::string outputPattern;
	std::string::const_iterator seek = Pattern.begin();

	// Première passe
	do {
		if(*seek == '-' && *(seek - 1) != '\\') {
			int start = static_cast<int>(*(seek - 1)) + 1;
			int end = static_cast<int>(*(seek + 1));

			if(end > start)
				for(int i = start; i <= end; i++) outputPattern += static_cast<char>(i);

			++seek;
		}else {
			outputPattern += *seek;
		}
	} while(++seek != Pattern.end());

	Pattern = outputPattern;
	seek = Pattern.begin();
	outputPattern.clear();

	std::cout << "outputPattern : " << outputPattern << std::endl;

	// Seconde passe
	do {
		std::cout << "Current Token : " << *seek << std::endl;
		
		if(*seek == '[' || *seek == '{') {
			char opening = (*seek);
			std::string::const_iterator oldSeek = seek;

			while(*seek != static_cast<char>(opening + 2)) ++seek;			// Cf ASCII

			if(*(seek + 1) == '?' || *(seek + 1) == '+' || *(seek + 1) == '*') {
				char Modifier = *(seek + 1);
				seek = oldSeek + 1;

				while(*seek != static_cast<char>(opening + 2)) {
					outputPattern += opening;
					outputPattern += Modifier;
					outputPattern += *(seek++);
					outputPattern += ',';
				}

				++seek;
			} else {
				seek = oldSeek + 1;

				while(*seek != static_cast<char>(opening + 2)) {
					outputPattern += opening;
					outputPattern += " ";
					outputPattern += *(seek++);
					outputPattern += ',';
				}
			}
		}else {
			if(*(seek + 1) == '?' || *(seek + 1) == '+' || *(seek + 1) == '*') {
				outputPattern += " ";
				outputPattern += *(seek + 1);
				outputPattern += *seek;
				outputPattern += ',';
				++seek;
			}else {
				outputPattern += " ";
				outputPattern += " ";
				outputPattern += *seek;
				outputPattern += ",";
			}
		}
	} while(++seek != Pattern.end());	

	return outputPattern;
}

